package com.bwie.controller;

import com.bwie.utils.R;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * TODO
 *
 * @Description
 * @Author 36167
 * @Date 2025/3/14 13:56
 **/


@RestController
@RequestMapping("/file")
public class Minio {

    @Autowired
    MinioClient minioClient;

    @PostMapping("/upload")
    public R upload(MultipartFile file){
        try {
            ObjectWriteResponse response = minioClient.putObject(PutObjectArgs.builder().bucket("xty")
                    .object(file.getOriginalFilename()).stream(file.getInputStream(), file.getSize(), -1).build());
            String url="http://127.0.0.1:9000/";
            String format=String.format("%s%s%s",url,response.bucket()+"/",response.object());
            return R.OK(format);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
